package com.example.recorder716;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "r716_Activity";

    String[] ps = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAPTURE_AUDIO_OUTPUT
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        findViewById(R.id.button_start_record).setOnClickListener( v -> startRecord());
        findViewById(R.id.button_stop_record).setOnClickListener( v -> stopRecord());


        for (int i = 0; i < ps.length; i++) {
            if (checkSelfPermission(ps[i]) != PackageManager.PERMISSION_GRANTED){
                requestPermissions(ps, 1);
                break;
            }
        }
    }

    private void startRecord() {
        Log.i(TAG, "startRecord");
        Intent i = new Intent(this, RecordService.class);
        i.setAction(RecordService.ACTION_START);
        startService(i);
    }

    private void stopRecord() {
        Log.i(TAG, "stopRecord");
        Intent i = new Intent(this, RecordService.class);
        i.setAction(RecordService.ACTION_STOP);
        startService(i);
    }
}